package com.wyh.blog.controller;


import com.wyh.blog.entity.Article;
import com.wyh.blog.entity.User;
import com.wyh.blog.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/userController")
@SessionAttributes(value = "user")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    /**
     * 验证账号密码是否正确
     */
    @GetMapping("/verifyAccount")
    @ResponseBody
    public Boolean verifyAccount(String name, String pwd, HttpSession session){
        System.out.println("开始验证账号密码");
        System.out.println(name+"=="+pwd);
        User user =  userService.verifyAccount(name,pwd);
        System.out.println(user);
        Boolean flag = true;
        if(user==null){
            flag = false;
        }
        session.setAttribute("user",user);
        return flag;

    }

    /**
     * 主人登陆成功跳转页面
     */
    @GetMapping("/getHome")
    public String getHome(Model model,HttpSession session){
        List<Article> articleList = userService.article();
        model.addAttribute("articleList",articleList);
        return "home";
    }


    /**
     * 游客通道
     */
    @GetMapping("/ykGetHome")
    public String ykGetHome(Model model){
        List<Article> articleList = userService.article();
        model.addAttribute("articleList",articleList);
        return "home";
    }
}
